#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ep emplace
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;
using tl = tuple < int, int, int >;

const int N = 2e5 + 10;
const int inf = 0x3f3f3f3f;

int h[N], a[N], b[N], Q, ans[N], n;
veg qpot[N];
vec padd[N], pdel[N];

namespace seg {
	int mx[N << 1], L[N << 1], R[N << 1], md[N << 1], ch[N << 1][2], val[N << 1], tg[N << 1], ndn, rt;
	void pt(int o, int v) { tg[o] = max(tg[o], v); val[o] = max(val[o], mx[o] + tg[o]); }
	void pd(int o) { if(tg[o] > -inf) pt(ch[o][0], tg[o]), pt(ch[o][1], tg[o]), tg[o] = -inf; }
	void build(int &o, int l, int r) {
		o = ++ndn; L[o] = l; R[o] = r; val[o] = mx[o] = tg[o] = -inf; if(l == r) return; md[o] = (L[o] + R[o]) >> 1;
		build(ch[o][0], L[o], md[o]); build(ch[o][1], md[o] + 1, R[o]);
	}
	int pos, v; 
	void updp(int o) {
		if(L[o] == R[o]) return mx[o] = v, tg[o] = -inf, void(); pd(o);
		if(pos <= md[o]) updp(ch[o][0]); else updp(ch[o][1]); mx[o] = max(mx[ch[o][0]], mx[ch[o][1]]);
		//val[o] = max(val[ch[o][0]], val[ch[o][1]]);
	}
	int xl, xr; 
	void updq(int o) {
		if(xl <= L[o] && R[o] <= xr) return pt(o, v); pd(o);
		if(xl <= md[o]) updq(ch[o][0]); if(xr > md[o]) updq(ch[o][1]);
		val[o] = max(val[ch[o][0]], val[ch[o][1]]); 
	}
	int query(int o) {
		if(xl <= L[o] && R[o] <= xr) return val[o]; pd(o); int res = -inf;
		if(xl <= md[o]) res = query(ch[o][0]); if(xr > md[o]) res = max(res, query(ch[o][1]));
		return res;
	}
	void updp(int pos, int v) { seg :: pos = pos; seg :: v = v; updp(rt); }
	void updq(int xl, int xr, int v) { seg :: xl = xl; seg :: xr = xr; seg :: v = v; updq(rt); }
	int query(int xl, int xr) { seg :: xl = xl; seg :: xr = xr; return query(rt); }
}

void solve() {
	seg :: ndn = 0; seg :: build(seg :: rt, 1, n);
	rep(i, 1, n) {
		if(a[i] + i <= n) padd[a[i] + i].pb(i);
		if(b[i] + i + 1 <= n) pdel[b[i] + i + 1].pb(i);
	}
	rep(i, 1, n) {
		for(auto x : padd[i]) seg :: updp(x, -h[x]);
		for(auto x : pdel[i]) seg :: updp(x, -inf);
		padd[i].clear(); pdel[i].clear();
		if(i - a[i] >= 1) seg :: updq(max(1, i - b[i]), i - a[i], h[i]);
		for(auto x : qpot[i]) ans[x.se] = max(ans[x.se], seg :: query(x.fi, i));
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in; rep(i, 1, n) h[i] = in, a[i] = in, b[i] = in;
	Q = in; rep(i, 1, Q) { int l = in, r = in; qpot[r].eb(l, i); ans[i] = -1; }
	solve(); rep(i, 1, n) h[i] = inf - h[i]; solve(); rep(i, 1, Q) printf("%d\n", ans[i]);
	return 0;
}
